package com.jh.controller;
/**
 * 代号:隐无为 2017：厚溥
 * 文件名：SubjectController.java
 * 创建人：柯栋
 * 日期：2017年9月21日
 * 修改人：
 * 描述：
 */


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.kd.common.base.controller.BaseController;
import org.kd.utils.JsonReturnData;
import org.kd.utils.PageTable;
import org.kd.utils.WebConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.jh.model.Subject;
import com.jh.model.Classify;
import com.jh.service.SubjectService;
import com.jh.service.ClassifyService;

/**
 * 用途：业务模块名称
 */
@Controller
@RequestMapping("/subjectController")
public class SubjectController extends BaseController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	SubjectService subjectService;
	@Autowired
	ClassifyService classifyService;


	@RequestMapping("/list")
	@RequiresPermissions("layim:init") //标记权限
	public String list(ModelMap modelMap,Classify classify) {
		modelMap.addAttribute("modle", classifyService.queryByParam(classify));
		logger.info("==============="+this.getClass().getName()+"==================");
		return "subject/list";
	}

	@RequestMapping("/saveUpdatePage")
	public String saveUpdatePage(Integer id, ModelMap modelMap) {
		if (id != null) {
			modelMap.put("subjectId", id);
		}
		return "subject/saveUpdatePage";
	}

	@RequestMapping("/saveUpdate")
	@ResponseBody
	public String saveUpdate(Subject subject) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "操作失败");
		if (subject.getId() != null) {
			subjectService.updateNoNull(subject);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("更新成功");
		} else {
			subject.setDelFlag(1);
			subjectService.insert(subject);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("添加成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Subject subject) {

		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "删除失败");
		if (subject.getId() != null) {
			subject.setDelFlag(WebConstant.DELETECODE);
			subjectService.updateDelete(subject);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/deletes")
	@ResponseBody
	public String deletes(String ids) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "批量删除失败");
		if (!StringUtils.isEmpty(ids)) {
//			subjectService.updateDeletes(ids);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("批量删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/ajaxList")
	@ResponseBody
	public String ajaxList(Integer page, Integer limit,Subject u) {
		PageTable<List<Subject>> pageTable = new PageTable<List<Subject>>();
		u.setDelFlag(WebConstant.NODELETECODE);
		PageInfo<Subject> pageInfo = subjectService.queryPageListByParam(page, limit, u);
		pageTable.setCode(WebConstant.PAGESUCCESSCODE);
		pageTable.setMsg("获取成功");
		pageTable.setData(this.resLength(pageInfo.getList()));
		pageTable.setCount(pageInfo.getTotal());
		return responseAPI.getJsonString(pageTable);
	}

	/**
	 *  @功能:查询答案
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年5月5日
	 *  @param subject
	 *  @return  
	 */
	@RequestMapping("/queryReslutByParam")
	@ResponseBody
	public String queryReslutByParam(Subject subject) {
		JsonReturnData<Subject> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "查询失败");
		subject.setDelFlag(WebConstant.NODELETECODE);
		Subject byParam = subjectService.queryReslutByParam(subject);
		jsonReturnData.setCode(WebConstant.SUCCESSCODE);
		jsonReturnData.setMsg(WebConstant.SUCCESS);
		jsonReturnData.setData(byParam);
		return responseAPI.getJsonString(jsonReturnData);
	}
	
	/**
	 *  @功能:根据id查询
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年5月6日
	 *  @param subject
	 *  @return  
	 */
	@RequestMapping("/queryById")
	@ResponseBody
	public String queryById(Subject subject) {
		JsonReturnData<Subject> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "查询失败");
		subject.setDelFlag(WebConstant.NODELETECODE);
		Subject model = subjectService.queryByParam(subject);
		jsonReturnData.setCode(WebConstant.SUCCESSCODE);
		jsonReturnData.setMsg(WebConstant.SUCCESS);
		jsonReturnData.setData(model);
		return responseAPI.getJsonString(jsonReturnData);
	}
	
	/**
	 *  @功能:组装答案长度
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年5月3日
	 *  @param list
	 *  @return  
	 */
	private List<Subject> resLength(List<Subject> list) {
		List<Subject> subjectList = new ArrayList<Subject>();
		Iterator<Subject> iterator = list.iterator();
		while (iterator.hasNext()) {
			Subject subject = (Subject) iterator.next();
			subject.setReslut(subject.getReslut().length()+"");
			subjectList.add(subject);
		}
		return subjectList;
	}
}
